package org.gp.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.gp.core.entity.Hobby;
import org.gp.core.entity.dto.ResponseDTO;
import org.gp.core.entity.enums.DTOCodeEnum;
import org.gp.core.entity.enums.HobbyEnum;
import org.gp.core.entity.result.HobbyArray;
import org.gp.core.entity.result.UserHobbyResult;
import org.gp.core.mapper.HobbyMapper;
import org.gp.core.service.IHobbyService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表  服务实现类
 * </p>
 *
 * @author LuoLiang
 * @since 2017-01-24
 */
@Service
@Slf4j
public class HobbyServiceImpl extends ServiceImpl<HobbyMapper, Hobby> implements IHobbyService {

    /**
     * 根据用户id查询用户的喜好
     *
     * @param userId
     * @return
     */
    @Override
    public ResponseDTO<UserHobbyResult> getHobbyByUser(Integer userId) {
        ResponseDTO<UserHobbyResult> dto = new ResponseDTO<>();

        EntityWrapper<Hobby> ew = new EntityWrapper<>();
        ew.where("user_id={0}", userId);

        Hobby hobby = selectOne(ew);

        //用户首次查看 兴趣爱好，没有数据
        if (hobby == null) {
            hobby = new Hobby();
            hobby.setHobbyStr("");
            hobby.setUserId(userId);
            insert(hobby);
        }

        UserHobbyResult userHobbyResult = new UserHobbyResult();
        List<UserHobbyResult.HobbyMap> hobbyMaps = new ArrayList<>();
        userHobbyResult.setHobbyMaps(hobbyMaps);
        if (null != hobby.getHobbyStr() && !hobby.getHobbyStr().equals("")) { //hobbyStr不为空，用户设置过兴趣爱好
            String[] hobbyKeys = hobby.getHobbyStr().split(",");  //分割解析字符串

            for (int i = 0; i < hobbyKeys.length; i++) {
                UserHobbyResult.HobbyMap hobbyMap = new UserHobbyResult().new HobbyMap();
                hobbyMap.setKey(hobbyKeys[i]);
                hobbyMap.setValue(HobbyEnum.HWLX.getDesc(hobbyKeys[i]));
                userHobbyResult.getHobbyMaps().add(hobbyMap);
            }
            dto.setData(userHobbyResult);
            dto.setCode(DTOCodeEnum.SUCCESS.getDesc());
        } else {
            dto.setCode(DTOCodeEnum.SUCCESS.getDesc());
            dto.setResMsg("当前用户没有设置爱好");
        }

        return dto;
    }

    @Override
    public ResponseDTO updateHobby(Integer userId, String hobbyStr) {
        ResponseDTO dto = new ResponseDTO();

        List<HobbyArray> list = JSON.parseObject(hobbyStr, new TypeReference<List<HobbyArray>>() {
        });
        String str = list.stream().map(HobbyArray::getKey).collect(Collectors.joining(","));
        log.info("id为{}的用户准备更新爱好为：{}", userId, str);

        EntityWrapper<Hobby> ew = new EntityWrapper<>();
        ew.where("user_id={0}", userId);
        try {
            Hobby hobby = selectOne(ew);

            if (hobby == null) {
                hobby = new Hobby();
                hobby.setUserId(userId);
            }
            hobby.setHobbyStr(str);

            insertOrUpdate(hobby);

            dto.setCode(DTOCodeEnum.SUCCESS.getDesc());
            dto.setResMsg("更新成功！");
        } catch (Exception e) {
            log.error("更新用户爱好出错", e);
            dto.setCode(DTOCodeEnum.FAIL.getDesc());
            dto.setResMsg("更新失败！");
        }

        return dto;
    }
}
